package com.east.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.east.config.listener.UploadDataListener;
import com.east.dto.ClueDTO;
import com.east.entity.Clue;
import com.east.entity.User;
import com.east.mapper.ClueMapper;
import com.east.query.ClueQuery;
import com.east.result.Result;
import com.east.service.ClueService;
import com.east.utils.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    private ClueMapper clueMapper;

    @Override
    public Result list(Integer currentPage, Integer pageSize, ClueQuery clueQuery) {
        Page<ClueDTO> page = PageHelper.startPage(currentPage, pageSize);
        List<ClueDTO> clueDTOS = clueMapper.list(clueQuery);
        PageData<ClueDTO> pageData = new PageData<ClueDTO>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);

    }

    @Override
    public Result saveFromExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ClueDTO.class, new UploadDataListener(clueMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.FAIL("导入失败");
        }
        return Result.OK();
    }

    @Override
    public Result save(ClueDTO clueDTO) {
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Clue clue = BeanUtil.copyProperties(clueDTO, Clue.class);
        clue.setCreateBy(loginUser.getId());
        clue.setCreateTime(new Date());
        int i = clueMapper.insertSelective(clue);
        return i > 0 ? Result.OK() : Result.FAIL("添加失败");
    }

    @Override
    public Result phoneExists(String phone, Integer clueId) {
        List<ClueDTO> clueDTOList = clueMapper.phoneExists(phone, clueId);
        if (clueDTOList == null || clueDTOList.isEmpty()) {
            return Result.OK();
        }
        return Result.FAIL("手机号已经存在");
    }

    @Override
    public Result clueInfo(Integer id) {
        Clue clue = clueMapper.selectByPrimaryKey(id.longValue());
        return Result.OK(clue);
    }

    @Override
    public Result update(Clue clue) {
        int i = clueMapper.updateByPrimaryKeySelective(clue);
        return i > 0 ? Result.OK() : Result.FAIL("更新失败");
    }

    @Override
    public Result clueCascadeInfo(Integer id) {
        ClueDTO clueDTO = clueMapper.selectCascadeByPrimaryKey(id.longValue());
        return Result.OK(clueDTO);
    }

    @Override
    public Result batchDelete(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.FAIL("请选择线索");
        }
        int i = clueMapper.batchDeleteByIds(ids);
        return i > 0 ? Result.OK() : Result.FAIL("删除失败");
    }

    @Override
    public Result updateState(Integer clueId,int state) {
        int i = clueMapper.updateState(clueId, state);
        return i > 0 ? Result.OK() : Result.FAIL("更新失败");
    }
}
